package com.jweb.framework.core.util;
import java.net.InetAddress;
import java.security.SecureRandom;
import java.util.Random;

public class GUID {
	
	private static String a = "ffff0066";
    private static Random b;
    private static SecureRandom c;
    private String d;
    private String e;
    private boolean f;
    
    private static String a(int i, int j)
    {
        String s = Integer.toHexString(i);
        if(s.length() < j)
        {
            StringBuffer stringbuffer = new StringBuffer();
            for(int k = 0; k < j - s.length(); k++)
                stringbuffer.append("0");

            stringbuffer.append(s);
            return stringbuffer.toString();
        } else
        {
            return s;
        }
    }

    

    static 
    {
        try
        {
            InetAddress inetaddress = InetAddress.getLocalHost();
            byte abyte0[] = inetaddress.getAddress();
            a = a(a(abyte0), 8);
        }
        catch(Exception _ex) { }
        c = new SecureRandom();
        long l = c.nextLong();
        b = new Random(l);
    }
	
	private static int a(byte abyte0[])
    {
        int i = abyte0[0] & -16777216;
        i += abyte0[1] & 16711680;
        i += abyte0[2] & 65280;
        i += abyte0[3] & 255;
        return i;
    }

    public GUID()
    {
        this(false);
    }

    public GUID(boolean flag)
    {
        f = flag;
        StringBuffer stringbuffer = new StringBuffer(a);
        stringbuffer.append(a(System.identityHashCode(this), 8));
        d = stringbuffer.toString();
        next();
    }

    public GUID next()
    {
        int i = (int)System.currentTimeMillis();
        int j = f ? c.nextInt() : b.nextInt();
        e = a(i, 8) + d + a(j, 8);
        return this;
    }

    public String toPlainString()
    {
        return e;
    }

    public String toString()
    {
        String s = toPlainString().toUpperCase();
        StringBuffer stringbuffer = new StringBuffer();
        stringbuffer.append(s.substring(0, 8));
        stringbuffer.append("-");
        stringbuffer.append(s.substring(8, 12));
        stringbuffer.append("-");
        stringbuffer.append(s.substring(12, 16));
        stringbuffer.append("-");
        stringbuffer.append(s.substring(16, 20));
        stringbuffer.append("-");
        stringbuffer.append(s.substring(20));
        return stringbuffer.toString();
    }

    public boolean equals(Object obj)
    {
        return (obj instanceof GUID) && ((GUID)obj).toPlainString().equals(toPlainString());
    }

    public int hashCode()
    {
        return toPlainString().hashCode();
    }

}
